import { login, logout, getInfo } from '@/api/login'
import { Message } from 'element-ui'
import secret from '@/utils/secret.js'
import { getToken, setToken, removeToken, getUserId, setUserName } from '@/utils/auth'
// 存取用户登录登出等信息
const user = {
  state: {
    token: getToken(),
    name: '',
    avatar: '',
    roles: [],
    assignment: [],
    userSystem: []
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_ASSIGNMENT: (state, assignment) => {
      state.assignment = assignment
    },
    SET_USERSYSTEM: (state, userSystem) => {
      state.userSystem = userSystem
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo).then(response => {
          // const data = response.data
          if (response.resultCode === '0004') {
            Message({
              message: response.resultDesc,
              type: 'error'
            })
          } else if (response.resultCode === '0003') {
            Message({
              message: response.resultDesc,
              type: 'error'
            })
          } else {
            setUserName(response.userName)
            commit('SET_NAME', response.userName)
            setToken(response.token)
            commit('SET_TOKEN', response.token)
            location.reload()
            resolve()
          }
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit, state }, baseUrl) {
      return new Promise((resolve, reject) => {
        const url = baseUrl + 'system/queryUserPower'
        const data = {
          // token: state.token,
          userId: getUserId('userId')
        }
        // console.log(data)
        getInfo(secret.Encrypt(data), url).then(response => {
          var sys = response.data.sysList
          var fun = response.data.funList
          /* if (fun.length > 0) {
            console.log(fun)
            fun.forEach(item => {
              if (item.powerType === 'menu' && item.powerCode.indexOf('usercenter'))
            })
          } */
          // 判断是否为空数组
          if (!fun) {
            fun = []
          }
          if (!sys) {
            sys = []
          }
          const routes = sys.concat(fun)
          var roles = [] // 按钮权限
          var rolePower = [] // 路由权限
          var left = []
          if (routes.length === 0) {
            roles = ['memberManage']
          } else {
            routes.forEach(function(item) {
              if (item.powerType === 'button' && item.powerCode.indexOf('usercenter') !== -1) {
                roles.push(item.powerCode)
              }
              if (item.powerType === 'menu') {
                rolePower.push(item.powerCode)
              }
              if (item.powerType === 'system') {
                left.push(item.powerCode)
              }
            })
          }
          commit('SET_USERSYSTEM', left)
          commit('SET_ROLES', rolePower)
          commit('SET_ASSIGNMENT', roles)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        // console.log(state.token)
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    }
  }
}

export default user
